﻿using System;
using Fly.Data;
using FlyWebFrame.Model;

namespace FlyWebFrame.Action.admin
{
    public class profiles : baseaction
    {
        public int CatId;
        public dynamic viewnews;
        public dynamic viewcat;

        public int lastindex;
        public int nextindex;
        public int preindex;
        public int pageindexview;
        public int countpage;

        public override void Treat()
        {
            contentleft = "/html/admin/profiles.htm";
            page_title = "LEAD Consulting - Quản lý bài giới thiệu";
            admin_script = "$('#ad_profiles').addClass('active');";
            
            // get newslist
            News news = new News();
            Categories newscat = new Categories();

            int pageindex;

            Int32.TryParse(GetParameter(0), out pageindex);
            pageindexview = pageindex + 1;

            string ids = "-1";
            viewcat = newscat.GetSubCatByType(1).Data;
            for (int i = 0; i < viewcat.Length; i++)
            {
                ids += "," + viewcat[i].id;
            }

            dynamic topcat = newscat.GetChild(0).Data;

            for (int i = 0; i < viewcat.Length; i++)
            {
                for (int j = 0; j < topcat.Length; j++)
                {
                    if (viewcat[i].parentId == topcat[j].id)
                    {
                        viewcat[i].parentTitle = topcat[j].name;
                        viewcat[i].parentViTitle = topcat[j].viName;
                        break;
                    }
                }
            }

            viewnews = news.GetByBigCat(ids, pageindex, 60).Data;
            for (int i = 0; i < viewnews.Length; i++)
            {
                for (int j = 0; j < viewcat.Length; j++)
                {
                    if (viewnews[i].catId == viewcat[j].id)
                    {
                        viewnews[i].title = viewcat[j].name;
                        viewnews[i].viTitle = viewcat[j].viName;
                        viewnews[i].parentTitle = viewcat[j].parentTitle;
                        break;
                    }
                }
            }

            Categories c = new Categories();
            countpage = 1;

            // paging
            if (countpage > 1)
            {
                admin_script += "$('#news-paging').removeClass('hidden')";
                if (pageindex >= countpage - 1)
                {
                    nextindex = pageindex;
                }
                else
                {
                    nextindex = pageindex + 1;
                }
                if (pageindex == 0)
                {
                    preindex = 0;
                }
                else
                {
                    preindex = pageindex - 1;
                }
                lastindex = countpage - 1;
            }
        }
    }
}